//
//  WOGroovyShell.java
//  WOGroovy
//  Written by Suzuki Tetsuya

package groovy.lang.webobjects.components;

import com.webobjects.appserver.*;
import com.webobjects.foundation.*;

import groovy.lang.*;
import groovy.lang.webobjects.WOGroovyBinding;

import org.codehaus.groovy.control.*;


import java.util.*;
import java.io.*;
import java.net.*;

public class WOGroovyShell extends GroovyShell implements Serializable {

	public WOGroovyShell() {
		super();
	}
	
	public WOGroovyShell(Binding binding) {
		super(binding);
	}
	
	public static WOGroovyShell shellWithDefaultBinding() {
		return new WOGroovyShell(WOGroovyBinding.defaultBinding());
	}
	
	public Binding binding() {
		return getContext();
	}
	
	public Object propertyForKey(String key) {
		return getProperty(key);
	}
	
	public void setPropertyForKey(Object value, String key) {
		setProperty(key, value);
	}
	
	public Object objectForKey(String key) {
		return getVariable(key);
	}
	
	public void setObjectForKey(Object value, String key) {
		setVariable(key ,value);
	}
	
	public Object evaluateWithResourceNamed(String aResourceName,
											String aFrameworkName,
											NSArray<String> aLanguageList)
		throws CompilationFailedException, IOException {
		try {
			InputStream in = WOApplication.application().resourceManager().
				inputStreamForResourceNamed(aResourceName,
											aFrameworkName,
											aLanguageList);
			return evaluate(in);
		} catch (CompilationFailedException cfe) {
			throw cfe;
		}
	}
	
	public Object evaluateWithContentOfFile(String path)
		throws CompilationFailedException, IOException {
		try {
			return evaluate(new File(path));
		} catch (CompilationFailedException cfe) {
			throw cfe;
		} catch (IOException ioe) {
			throw ioe;
		}
	}
	
	public void executeWithResourceNamed(String aResourceName,
										 String aFrameworkName,
										 NSArray<String> aLanguageList,
										 NSArray<String> args)
		throws CompilationFailedException, IOException {
		try {
			URL pathURL = WOApplication.application().resourceManager().
				pathURLForResourceNamed(aResourceName,
										aFrameworkName,
										aLanguageList);
			File file = new File(pathURL.getPath());
			ArrayList<String> list = new ArrayList<String>(args.vector());
			run(file, list);
		} catch (CompilationFailedException cfe) {
			throw cfe;
		} catch (IOException ioe) {
			throw ioe;
		}
	}
	
	public void executeWithContentOfFile(File file, NSArray<String> args)
		throws CompilationFailedException, IOException {
		try {
			ArrayList<String> list = new ArrayList<String>(args.vector());
			run(file, list);
		} catch (CompilationFailedException cfe) {
			throw cfe;
		} catch (IOException ioe) {
			throw ioe;
		}
	}
	
	public void executeWithContentOfFile(String path, NSArray<String> args)
		throws CompilationFailedException, IOException {
		try {
			ArrayList<Object> list = new ArrayList<Object>(args.vector());
			run(new File(path), list);
		} catch (CompilationFailedException cfe) {
			throw cfe;
		} catch (IOException ioe) {
			throw ioe;
		}
	}
	
	public Object executeWithStream(InputStream in, String fileName, NSArray<String> args)
		throws CompilationFailedException, IOException {
		try {
			return run(in, fileName, args.objects());
		} catch (CompilationFailedException cfe) {
			throw cfe;
		}
	}
	
	public void executeWithScript(String scriptText, String fileName, NSArray<String> args)
		throws CompilationFailedException, IOException {
		try {
			run(scriptText, fileName, args.objects());
		} catch (CompilationFailedException cfe) {
			throw cfe;
		}
	}

	public Script scriptWithResourceNamed(String aResourceName,
										  String aFrameworkName,
										  NSArray<String> aLanguageList)
		throws CompilationFailedException, IOException {
		try {
			URL pathURL = WOApplication.application().resourceManager().
				pathURLForResourceNamed(aResourceName,
										aFrameworkName,
										aLanguageList);
			File file = new File(pathURL.getPath());
			return parse(file);
		} catch (CompilationFailedException cfe) {
			throw cfe;
		} catch (IOException ioe) {
			throw ioe;
		}
	}
	
	public Script scriptWithContentOfFile(String path)
		throws CompilationFailedException, IOException {
		try {
			return parse(new File(path));
		} catch (CompilationFailedException cfe) {
			throw cfe;
		} catch (IOException ioe) {
			throw ioe;
		}
	}
}
